using AutoMapper;
using B.S.Sterilizationcenter.Domain.yjb;
using B.S.Sterilizationcenter.ErrorCode;
using B.S.Sterilizationcenter.Read.API.Appliaction.Commends.yjb;
using MediatR;
using SqlSugar;

namespace B.S.Sterilizationcenter.Read.API.Appliaction.CommandHandlers.yjb
{
    public class ShowPrescriptionCheckCommandHandler : IRequestHandler<ShowPrescriptionCheckCommand, ApiResult<List<PrescriptionCheck>>>
    {
        private readonly ISqlSugarClient db;
        private readonly IMapper mapper;

        public ShowPrescriptionCheckCommandHandler(ISqlSugarClient db, IMapper mapper)
        {
            this.db = db;
            this.mapper = mapper;
        }

        /// <summary>
        /// 查看检药单
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<ApiResult<List<PrescriptionCheck>>> Handle(ShowPrescriptionCheckCommand request, CancellationToken cancellationToken)
        {
            try
            {
                ApiResult<List<PrescriptionCheck>> result = new ApiResult<List<PrescriptionCheck>>();
                var list = db.Queryable<PrescriptionCheck>().Where(x => x.IsDeleted == false).ToList();

                if (request.PrescriptionCheckId.HasValue)
                {
                    list = list.Where(it => it.PrescriptionCheckId == request.PrescriptionCheckId).ToList();
                }
                if (!string.IsNullOrEmpty(request.RegistrationNumber))
                {
                    list = list.Where(it => it.RegistrationNumber.Contains(request.RegistrationNumber)).ToList();
                }
                if (!string.IsNullOrEmpty(request.PatientName))
                {
                    list = list.Where(it => it.PatientName.Contains(request.PatientName)).ToList();
                }
                if (!string.IsNullOrEmpty(request.CardNumber))
                {
                    list = list.Where(it => it.CardNumber.Contains(request.CardNumber)).ToList();
                }

                result.Code = ApiEnums.查询成功;
                result.Msg = "查询成功";
                result.Data = list;
                return result;
            }
            catch (Exception)
            {
                throw;
            }
        }
    }
} 